﻿using System;
using System.IO;
using System.Linq;
using System.Numerics;
using MadLibrary.Collections;
using MadLibrary.IO;

namespace Examples
{
    class Program
    {
        static void Main(string[] args)
        {
            IO_Util();
        }

        //Collections
        /// <summary>
        /// This example code show the usage of the BinarySearchTree class.
        /// In this example, T is Int32 but can be any type.
        /// Types implementing IComparable will be sorted using the CompareTo method.
        /// Types not implementing IComparable will be sorted using their hashcode.
        /// </summary>
        static void BinarySearchTree()
        {
            //Initialization.
            BinarySearchTree<int> tree = new BinarySearchTree<int>();
            //uncomment the following line to init the tree with pre-added values.
            tree = new BinarySearchTree<int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

            //Adding values to the tree.
            tree.Add(3);
            tree.Add(-123);

            //Removes a value from the tree.
            tree.Remove(3);

            //Iterates over the tree and prints it.
            //Items will be printed sorted.
            Console.WriteLine("Nodes:");
            tree.Map(Console.WriteLine);
            Console.WriteLine();
            /*
             * .Map is a MadLibrary extension method.
             * The following could be established with the following.
             * foreach(var value in tree)
             *      Console.WriteLine(value);
             */
            //Prints the height of the tree.
            Console.WriteLine("Height: {0}", tree.Height);

            //Prints the count of nodes in the tree and their types
            Console.WriteLine("Nodes: {0}\n{{\n\tLeaf: {1}\n\tBranch: {2}\n}}", tree.Count, tree.CountLeaf, tree.Count - tree.CountLeaf);
        }

        /// <summary>
        /// This example demonstrates the methods of the CollectionUtilites class.
        /// </summary>
        static void CollectionUtilities()
        {
            //This code writes the bit value of 'value' into 'bools'.
            bool[] bools = new bool[1024];
            BigInteger value = new BigInteger(ulong.MaxValue) / 97;
            Console.WriteLine(value);
            Console.ReadKey();

            //Decompression.
            bools.Decompress(value, 0);

            //An example of the Map method.
            //This method takes a delegate and applies it to all elements of a collection.
            bools.Map(Console.WriteLine);
            Console.WriteLine();
            //Shuffle shuffles
            bools = bools.Shuffle().ToArray();

            //Compress takes the values of the array and orders them back into bits.
            value = bools.Compress();
            Console.WriteLine(value);
        }

        //IO
        /// <summary>
        /// This example demonstrates the methods of the IOUtil class.
        /// </summary>
        static void IO_Util()
        {
            //This tree is just for reference.
            BinarySearchTree<int> tree = new BinarySearchTree<int>(124, 312, 532);


            //This method is used to generate a byte represntation of the object.
            //The object has to be serializbe and 
            byte[] serializedTree = tree.ToArray().GenerateBytes();
            //Create a new folder at the user directory.
            string path = Path.Combine(IOUtil.UserFolder, @"test");
            Directory.CreateDirectory(path);

            //Create 10 files with different names.
            for (int i = 0; i < 10; i++)
            {
                File.WriteAllBytes(Path.Combine(path, (i * 31).ToString()), serializedTree);
            }
            //Change all files' names using a format string.
            IOUtil.MassChangeFileName(path, "testFile{0}.bin", null);
        }

        //Managing
    }
}
